using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Services;
using MainGame.GameSound;
using Microsoft.Phone.Tasks;

namespace MainGame.Menu
{

  public interface ICreditsMenu
  {
    event MenuEvent OnCBackClicked;
  }

  public class CreditsMenu : BaseMenu, ICreditsMenu
  {
    SpriteBatch spriteBatch;

    private Button BackToMM, btnEmail, btnWebsite, btnBugs, btnCover, btnCoverUp;
    private Vector2 savedPos;
    public bool Animated = false;    
    public bool credit = false;
    private Color color, color2 = Color.White;
    private float skySplash = 15f;
    private bool btnclick;

    //Tasks
    private WebBrowserTask task;
    private EmailComposeTask mailTask;

    private Texture2D logo, box;
    public int Direction = 1;
    private int scrollY = 480, scrollX = 0, AnimVelocity = 60;
    
    public CreditsMenu(Game game, MenuDepths depth)
      : base(game, depth)
    {
      background = null;
      bounds = new Rectangle(70, 10, 220, 331);
      game.Services.AddService(typeof(ICreditsMenu), this);
    }

    protected override void Dispose(bool disposing)
    {
      Game.Services.RemoveService(typeof(ICreditsMenu));
      base.Dispose(disposing);
    }


    public override void Initialize()
    {
        spriteBatch = Game.GetContext().SpriteBatch;
        savedPos.X = bounds.X;
        btnclick = false;

        logo = Game.Content.Load<Texture2D>(@"Images\\Menu\\Logo");
        box = Game.Content.Load<Texture2D>(@"Images\\Menu\\RoundedBox");

        color = Color.Black;
        color.A = 0;

        task = new WebBrowserTask();
        mailTask = new EmailComposeTask();



        btnCover = new Button(Game, @"Images//Menu//CoverBlack", new Rectangle(-80, 405, 1000, 80), Color.Transparent, "small_font", "", Color.Black);
        AddButton(btnCover, () =>
        {
        });

        btnCoverUp = new Button(Game, @"Images//Menu//CoverBlack", new Rectangle(-60, -5, 1000, 40), Color.Transparent, "small_font", "", Color.Black);
        AddButton(btnCoverUp, () =>
        {
        });

        BackToMM = new Button(Game, @"Images//Menu//button2", new Rectangle(640, 420, 120, 55), Color.Transparent, "small_font", Locale.Strings.Back, Color.Transparent);
        AddButton(BackToMM, () =>
        {
            // Save the current pageGame
            Game.GetContext().currentConfig.currentPage = GamePage.main;
            SaveData.SaveUtils.SaveConfig(Game, Game.GetContext().currentConfig);

            BackToMM.touched = true;
            var SoundManager = Game.Services.GetService(typeof(ISoundManager<MenuSounds>)) as ISoundManager<MenuSounds>;
            SoundManager.PlaySound(MenuSounds.ClickCard);
        });

        btnWebsite = new Button(Game, @"Images//Menu//button2", new Rectangle(60, 420, 120, 55), Color.Transparent, "small_font", Locale.Strings.Website, Color.Transparent);
        AddButton(btnWebsite, () =>
        {
            task.URL = "www.carouselteam.com";
            //btnclick = true;
            task.Show();
        });

        btnEmail = new Button(Game, @"Images//Menu//button2", new Rectangle(260, 420, 120, 55), Color.Transparent, "small_font", Locale.Strings.Email, Color.Transparent);
        AddButton(btnEmail, () =>
        {
            mailTask.To = "info@carousel.com";
            mailTask.Show();
        });



        btnBugs = new Button(Game, @"Images//Menu//button2", new Rectangle(460, 420, 120, 55), Color.Transparent, "small_font", Locale.Strings.Bugs, Color.Transparent);
        AddButton(btnBugs, () =>
        {
            mailTask.To = "info@carousel.com";
            mailTask.Show();
        });


        base.Initialize();
    }

    private int counter = 4;

    public override void Update(GameTime gameTime)
    {
        if (BackToMM.touched)
        {
            counter--;
            if (counter < 0)
            {
                Animated = true;
                credit = true;
            }

        }

        if (menuDepth == MenuDepths.ForeGround)
        {
            BackToMM.color = Color.MediumBlue;
            BackToMM.textColor = Color.White;
            btnCover.color = Color.White;
            btnCoverUp.color = Color.White;
            btnBugs.color = Color.Red;
            btnEmail.color = Color.Orange;
            btnWebsite.color = Color.Green;
            btnBugs.textColor = Color.White;
            btnEmail.textColor = Color.White;
            btnWebsite.textColor = Color.White;

        }

        // Input: Back button pressed
        if (((Game1)Game).IsBackButtonPressed())
        {
            //if (btnclick)
            //{
            //  Animated = false;
            //  credit = true;
            //  btnclick = false;
            //}
            //else
            //{
            Animated = true;
            credit = true;
            //}

        }

        if (Animated)
        {
            bounds.X += Direction * AnimVelocity;


            if (bounds.X > (savedPos.X + bounds.Width + 30))
            {

                Direction = -1;
                if (menuDepth == MenuDepths.BackMenu)
                {
                    menuDepth = MenuDepths.ForeGround;
                    DrawOrder = (int)menuDepth;
                }
                else
                {
                    menuDepth = MenuDepths.BackMenu;
                    DrawOrder = (int)menuDepth;
                }
            }

            if (bounds.X < savedPos.X)
            {
                int offset = (int)savedPos.X - bounds.X;
                Animated = false;
                Direction = 1;
                bounds.X = (int)savedPos.X;

                foreach (Button b in (from cb in buttons select cb.b))
                {
                    //if (!b.message.Equals(Locale.Strings.Back))
                    //  b.bounds.X += Direction * offset;
                }

                if (menuDepth == MenuDepths.BackMenu)
                {
                    // Save the current pageGame
                    Game.GetContext().currentConfig.currentPage = GamePage.main;
                    SaveData.SaveUtils.SaveConfig(Game, Game.GetContext().currentConfig);

                    if (OnCBackClicked != null)
                        OnCBackClicked();
                }

            }

        }

        if ((menuDepth == MenuDepths.ForeGround) || (credit))
        {
            if ((color.A < 230) && (!credit))
                color.A += 10;

            if (credit)
                color.A -= 10;
        }

        base.Update(gameTime);
    }


    public override void Draw(GameTime gameTime)
    {
        if (menuDepth == MenuDepths.ForeGround)
        {
            spriteBatch.Begin();

            spriteBatch.Draw(Game.Content.Load<Texture2D>(@"white_pixel"), new Rectangle(0, 0, 800, 480), color);

            spriteBatch.Draw(box, new Rectangle(70, 80, 270, 250), null, Color.Black, 0, Vector2.Zero, SpriteEffects.None, (float)DrawOrder / (float)MenuDepths.numMenus);

            spriteBatch.Draw(logo, new Rectangle(110, 80, 190, 250), null, Color.White, 0, Vector2.Zero, SpriteEffects.None, (float)DrawOrder / (float)MenuDepths.numMenus);

            spriteBatch.DrawString(Game.Content.Load<SpriteFont>("baveuse1_font"), Locale.Strings.Programmers, new Vector2(310 + scrollX, bounds.Y + scrollY), Color.Red);
            spriteBatch.DrawString(Game.Content.Load<SpriteFont>("baveuse1_font"), Locale.Strings.CreditsProgrammers.Replace("\\n", "\n"), new Vector2(330 + scrollX, bounds.Y + scrollY + 40), Color.White);
            spriteBatch.DrawString(Game.Content.Load<SpriteFont>("baveuse1_font"), Locale.Strings.Coordinator, new Vector2(310 + scrollX, bounds.Y + scrollY + 180), Color.Red);
            spriteBatch.DrawString(Game.Content.Load<SpriteFont>("baveuse1_font"), Locale.Strings.CreditsCoordinator, new Vector2(330 + scrollX, bounds.Y + scrollY + 220), Color.White);
            //spriteBatch.DrawString(Game.Content.Load<SpriteFont>("baveuse1_font"), Locale.Strings.Graphics, new Vector2(310 + scrollX, bounds.Y + scrollY + 255), Color.Red);
            spriteBatch.DrawString(Game.Content.Load<SpriteFont>("baveuse1_font"), Locale.Strings.SpecialThanks, new Vector2(310 + scrollX, bounds.Y + scrollY + 280), Color.Red);
            spriteBatch.DrawString(Game.Content.Load<SpriteFont>("baveuse1_font"), Locale.Strings.CreditsGraphics.Replace("\\n", "\n"), new Vector2(330 + scrollX, bounds.Y + scrollY + 320), Color.White);
            //spriteBatch.DrawString(Game.Content.Load<SpriteFont>("baveuse1_font"), Locale.Strings.Graphics.Replace("\\n", "\n"), new Vector2(330 + scrollX, bounds.Y + scrollY + 295), Color.White);
            //spriteBatch.DrawString(Game.Content.Load<SpriteFont>("small_font"), Locale.Strings.TeamEmail.Replace("\\n", "\n"), new Vector2(20, bounds.Y + 380), Color.Red, 0, Vector2.Zero, skySplash, SpriteEffects.None, 0);

            spriteBatch.End();

            if ((bounds.X >= 240) && (credit))
                scrollX += 60;

            scrollY -= 3;
            if (scrollY < -500)
                scrollY = 500;

            if (skySplash > 1f)
                skySplash -= 0.3f;
        }

        base.Draw(gameTime);

    }

    // Event
    public event MenuEvent OnCBackClicked;
  }
}
